# -*- coding: utf-8 -*-

from gtk import *
from preview import *

class PhotoFormat(HBox):
	def format_changed(self, iconview):
		format = iconview.get_model()[iconview.get_selected_items()[0][0]]
		if format[2] > 0:
			self.width_spin.set_value(format[2])
			self.height_spin.set_value(format[3])
			
	
	def dimension_changed(self, spinbutton):
		self.prev_frame.set(xalign=0.5, yalign=0.5, ratio=self.width_spin.get_value() / self.height_spin.get_value(), obey_child=False);
		for i in range(len(self.format_model)):
			row = self.format_model[i]
			if self.width_spin.get_value() == row[2] and self.height_spin.get_value() == row[3] and not self.format_iv.path_is_selected(i):
				self.format_iv.select_path(i)
				return
		self.format_iv.select_path(len(self.format_model) - 1)
	
	def __init__(self, ppm):
		HBox.__init__(self)
		self.ppm = ppm
		vb = VBox()
		format_frame = Frame("Format de papier")
		self.format_model = ListStore(gdk.Pixbuf, str, float, float)
		self.format_model.append((gdk.pixbuf_new_from_file("../img/100x150.png"), "4\" x 6\"\n10 x 15 cm", 100.0, 150.0))
		self.format_model.append((gdk.pixbuf_new_from_file("../img/100x150.png"), "5\" x 7\"\n13 x 18 cm", 130.0, 180.0))
		self.format_model.append((gdk.pixbuf_new_from_file("../img/100x150.png"), "Carte de crédit", 54.0, 86.0))
		self.format_model.append((gdk.pixbuf_new_from_file("../img/100x150.png"), "Lettre\n8,5\" x 11\"", 216.0, 279.0))
		self.format_model.append((gdk.pixbuf_new_from_file("../img/100x150.png"), "A4", 210.0, 297.0))
		self.format_model.append((gdk.pixbuf_new_from_file("../img/100x150.png"), "Personnalisé", 0.0, 0.0))
		self.format_iv = IconView(self.format_model)
		self.format_iv.set_pixbuf_column(0)
		self.format_iv.set_text_column(1)
		self.format_iv.set_selection_mode(SELECTION_BROWSE)
		self.format_iv.select_path(0)
		self.format_iv.connect("selection-changed", self.format_changed)
		format_sw = ScrolledWindow()
		format_sw.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
		format_sw.add(self.format_iv)
		format_frame.add(format_sw)
		vb.pack_start(format_frame, expand=True)
		
		orientation_frame = Frame("Orientation du papier")
		auto = RadioButton(label="Automatique")
		portrait = RadioButton(group=auto, label=STOCK_ORIENTATION_PORTRAIT)
		portrait.set_use_stock(True)
		landscape = RadioButton(group=auto, label=STOCK_ORIENTATION_LANDSCAPE)
		landscape.set_use_stock(True)
		vb_ori = VBox()
		vb_ori.add(auto)
		vb_ori.add(portrait)
		vb_ori.add(landscape)
		orientation_frame.add(vb_ori)
		hb_ori_dim = HBox(homogeneous=True)
		hb_ori_dim.pack_start(orientation_frame)
		dimension = Frame("Dimension")
		table_dim = Table(rows=2, columns=3, homogeneous=False)
		table_dim.attach(Label("Largeur :"), left_attach=0, right_attach=1, top_attach=0, bottom_attach=1)
		self.width_spin = SpinButton(adjustment=Adjustment(value=100, lower=1, upper=10000, step_incr=1, page_incr=10, page_size=0))
		self.width_spin.connect("value-changed", self.dimension_changed)
		table_dim.attach(self.width_spin, left_attach=1, right_attach=2, top_attach=0, bottom_attach=1, xoptions=EXPAND)
		table_dim.attach(Label("mm"), left_attach=2, right_attach=3, top_attach=0, bottom_attach=1)
		table_dim.attach(Label("Hauteur :"), left_attach=0, right_attach=1, top_attach=1, bottom_attach=2)
		self.height_spin = SpinButton(adjustment=Adjustment(value=150, lower=1, upper=10000, step_incr=1, page_incr=10, page_size=0))
		self.height_spin.connect("value-changed", self.dimension_changed)
		table_dim.attach(self.height_spin, left_attach=1, right_attach=2, top_attach=1, bottom_attach=2, xoptions=EXPAND)
		table_dim.attach(Label("mm"), left_attach=2, right_attach=3, top_attach=1, bottom_attach=2)
		dimension.add(table_dim)
		hb_ori_dim.pack_start(dimension)
		
		vb.pack_start(hb_ori_dim, expand=False)
		
		layout_frame = Frame("Agencement des images")
		complete = VBox()
		complete.add(image_new_from_file("../img/full.png"))
		l_complete = Label("Complètes en \ngardant le ratio")
		l_complete.set_justify(JUSTIFY_CENTER)
		complete.add(l_complete)
		b_complete = RadioButton()
		b_complete.set_mode(False)
		b_complete.set_relief(RELIEF_NONE)
		b_complete.add(complete)
		recrop = VBox()
		recrop.add(image_new_from_file("../img/cropped.png"))
		l_recrop = Label("Recadrées")
		l_recrop.set_justify(JUSTIFY_CENTER)
		recrop.add(l_recrop)
		b_recrop = RadioButton(b_complete)
		b_recrop.set_mode(False)
		b_recrop.set_relief(RELIEF_NONE)
		b_recrop.add(recrop)
		complete2 = VBox()
		complete2.add(image_new_from_file("../img/full-resized.png"))
		l_complete2 = Label("Complètes en \npleine page")
		l_complete2.set_justify(JUSTIFY_CENTER)
		complete2.add(l_complete2)
		b_complete2 = RadioButton(b_recrop)
		b_complete2.set_mode(False)
		b_complete2.set_relief(RELIEF_NONE)
		b_complete2.add(complete2)
		
		layout_hb = HButtonBox()
		layout_hb.add(b_recrop)
		layout_hb.add(b_complete)
		layout_hb.add(b_complete2)
		
		layout_frame.add(layout_hb)
		vb.pack_start(layout_frame, expand=False)
		self.pack_start(vb, expand=False)
		preview_frame = Frame("Aperçu")
		vb_preview = VBox()
		self.prev_frame = AspectFrame(ratio=self.width_spin.get_value() / self.height_spin.get_value(), obey_child=False)
		self.prev_frame.set_shadow_type(SHADOW_NONE)
		self.preview = Preview(ppm)
		self.prev_frame.add(self.preview)
		vb_preview.add(self.prev_frame)
		mini_browser = HBox()
		mini_browser.set_spacing(5)
		mini_browser.pack_start(Label("Page :"), expand=False)
		self.preview_page = SpinButton(adjustment=Adjustment(value=1, lower=1, upper=0, step_incr=1, page_incr=10, page_size=0))
		self.preview_page.connect("value-changed", self.changed)
		mini_browser.pack_start(self.preview_page, expand=False)
		self.count_page = Label("/ 1")
		mini_browser.pack_start(self.count_page, expand=False)
		frame_mini = Alignment(xalign=0.5)
		frame_mini.add(mini_browser)
		vb_preview.pack_start(frame_mini, expand=False)
		preview_frame.add(vb_preview)
		self.pack_start(preview_frame, expand=True)
		
		self.ppm.list.connect("row-changed", self.changed)
		self.ppm.list.connect("row-deleted", self.changed)
		self.ppm.list.connect("row-has-child-toggled", self.changed)
		self.ppm.list.connect("row-inserted", self.changed)
		self.ppm.list.connect("rows-reordered", self.changed)
		
	def changed(self, *params):
		page = self.preview_page.get_value_as_int()
		self.preview.lignes = self.ppm.browser.lignes.get_value_as_int()
		self.preview.colonnes = self.ppm.browser.colonnes.get_value_as_int()
		nb_per_page = self.preview.lignes * self.preview.colonnes
		first = (page - 1) * nb_per_page
		
		count = 0
		self.preview.pbs = []
		for row in self.ppm.list:
			for i in range(row[COLUMN_NB_COPY]):
				if count in range(first, first + nb_per_page):
					self.preview.pbs.append(row[COLUMN_PIXBUF])
				count += 1
		
		pages = count / nb_per_page
		self.count_page.set_text("/ " + str(pages))
		self.preview_page.set_range(1, pages)
		
		self.preview.queue_draw()
